#include <iostream>
#include <string>
#include <unistd.h>

using namespace std;

const int num = 5;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void* active(void* args)
{
    string name = static_cast<const char*>(args);
    while (true)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex);
        cout << name << " 活动" << endl;
        pthread_mutex_unlock(&mutex);
    }
}

int main()
{
    pthread_t tid[num];
    for (int i = 0; i < num; ++i)
    {
        char* name = new char[64];
        snprintf(name, 64, "thread-%d", i);
        pthread_create(&tid[i], nullptr, active, name);
    }

    while (true)
    {
        // pthread_cond_signal(&cond);
        pthread_cond_broadcast(&cond);
        sleep(1);
    }

    for (int i = 0; i < num; ++i)
    {
        pthread_join(tid[i], nullptr);
    }
    return 0;
} 